package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

// 登录弹窗
func (app *config) login(win fyne.Window) {

	w := fyne.CurrentApp().NewWindow("登录")

	zhanwei := widget.NewLabel("")
	username := widget.NewLabel("username: ")
	usernameInput := widget.NewEntry()
	usernameRow := container.NewBorder(zhanwei, nil, username, layout.NewSpacer(), usernameInput)

	password := widget.NewLabel("password: ")
	passwordInput := widget.NewPasswordEntry()
	passwordRow := container.NewBorder(nil, layout.NewSpacer(), password, layout.NewSpacer(), passwordInput)

	fmt.Println(usernameInput.Text)
	saveBtn := widget.NewButton("登录", func() {
		if usernameInput.Text == "" {
			toast("请输入用户名", w)
			return
		}
		if passwordInput.Text == "" {
			toast("请输入密码", w)
			return
		}
		if usernameInput.Text == "123" && passwordInput.Text == "123" {
			w.Hide()
			win.Show()
			defer w.Close()
			// return
		} else {
			toast("用户名或密码错误", w)
			return
		}
	})
	// saveBtn.OnTapped = app.loginFunc(win, w, usernameInput.Text, passwordInput.Text)

	loginLayout := container.New(layout.NewVBoxLayout(), usernameRow, passwordRow, saveBtn)
	w.SetContent(loginLayout)

	// 设置窗口大小,
	w.Resize(fyne.NewSize(320, 200))
	w.CenterOnScreen()
	w.Show()

}
func (app *config) loginFunc(win fyne.Window, w fyne.Window, usernameInput string, passwordInput string) func() {
	return func() {
		fmt.Println(usernameInput)
		if usernameInput == "" {
			toast("请输入用户名", w)
			return
		}
		if passwordInput == "" {
			toast("请输入密码", w)
			return
		}
		if usernameInput == "123" && passwordInput == "123" {
			w.Hide()
			win.Show()
			defer w.Close()
			// return
		} else {
			toast("用户名或密码错误", w)
			return
		}

	}
}
